<template>
  <div class="goods-list">

    <!-- <router-link class="goods-item" v-for="(item,index) in goodsList" :key="index" :to="'/home/goodsInfo/'+item.id" tag="div">
      <img :src="item.imagUrl" alt height="200" width="200" />
      <h1 class="title">{{item.title}}</h1>
      <div class="info">
        <p class="price">
          <span class="now">￥{{item.sellPrice}}</span>
          <span class="old">￥{{item.marketPrice}}</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩余{{item.stockQuantity}}件</span>
        </p>
      </div>
    </router-link> -->

    <div class="goods-item" v-for="(item,index) in goodsList" :key="index"
    @click="goDetail(item.id)">
      <img :src="item.imagUrl" alt height="200" width="200" />
      <h1 class="title">{{item.title}}</h1>
      <div class="info">
        <p class="price">
          <span class="now">￥{{item.sellPrice}}</span>
          <span class="old">￥{{item.marketPrice}}</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩余{{item.stockQuantity}}件</span>
        </p>
      </div>
    </div>

    <mt-button type="danger" size="large" @click="getMore">加载更多</mt-button>
  </div>
</template>

<script>
import { Toast } from "mint-ui";
export default {
  data() {
    return {
      goodsList: [],
      id:1
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    getGoodsList() {
      this.$http.get("/src/data/imgShare/goodsList.json").then(result => {
        if (result.status === 200) {
          this.goodsList = this.goodsList.concat(result.body.messages);
        } else {
          Toast("获取商品信息失败！");
        }
      });
    },
    getMore(){
        this.id++;
        console.log("这是第"+this.id+"页");
        this.getGoodsList();
    },
    // 网页跳转有两种方式
    // 方式1：使用a标签的形式叫做 标签跳转
    // 方式2：使用window.loaction.href的形式叫做 编程式导航
    // this.$route是路由参数对象：所有路由中的参数params和query都属于他
    // this.$router是一个路由导航对象，用它可以使用js代码实现路由的前进 后退 跳转
    goDetail(id){
        this.$router.push('/home/goodsInfo/'+id);
    }
  }
};
</script>

<style lang="scss">
.goods-list {
  display: flex;
  flex-wrap: wrap;
  padding: 7px;
  justify-content: space-between;

  .goods-item {
    width: 49%;
    border: 1px solid #ccc;
    box-shadow: 0 0 8px #ccc;
    margin: 4px 0;
    padding: 2px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    min-height: 163px;
    img {
      width: 100%;
    }
    .title {
      font-size: 14px;
    }
    .info {
      background-color: #eee;
      p {
        margin: 0;
        padding: 5px;
      }
      .price {
        .now {
          color: red;
          font-weight: bold;
          font-size: 16px;
        }
        .old {
          text-decoration: line-through;
          font-size: 12px;
          margin-left: 10px;
        }
      }
      .sell {
        display: flex;
        justify-content: space-between;
        font-size: 12px;
      }
    }
  }
}
</style>